package com.javatest.app;

import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Objects;
import java.util.Properties;

import static org.quartz.SimpleScheduleBuilder.simpleSchedule;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author liuziyang
 * @create 2020-02-01 12:28
 */
@Slf4j
public class App {
    public static void main(String[] args) {
        try {
            // Grab the Scheduler instance from the Factory
            Scheduler scheduler1 = StdSchedulerFactory.getDefaultScheduler();

            Properties config = new Properties();
            config.setProperty("org.quartz.scheduler.instanceName", "Scheduler2");
            config.setProperty("org.quartz.threadPool.threadCount", "10");
            config.setProperty("org.quartz.jobStore.class", "org.quartz.simpl.RAMJobStore");

            Objects.requireNonNull(config);

            StdSchedulerFactory ssf = new StdSchedulerFactory(config);
            Scheduler scheduler2 = ssf.getScheduler();

            log.info("scheduler1 equals scheduler2 ? {}", scheduler1.equals(scheduler2));

            // and start it off
            scheduler1.start();
            scheduler2.start();

            // define the job and tie it to our HelloJob class
            JobDetail job = JobBuilder.newJob(HelloJob.class)
                    .withIdentity("job1", "group1")
                    .build();

            // Trigger the job to run now, and then repeat every 40 seconds
            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity("trigger1", "group1")
                    .startNow()
                    .withSchedule(simpleSchedule()
                            .withIntervalInSeconds(3)
                            .repeatForever())
                    .build();

            // Tell quartz to schedule the job using our trigger
            scheduler1.scheduleJob(job, trigger);
            scheduler2.scheduleJob(job, trigger);

//            scheduler.shutdown();

        } catch (SchedulerException se) {
            se.printStackTrace();
        }
    }

}
